﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using PKURG.CostSplit.SyncService.DAL;

namespace PKURG.CostSplit.SyncService
{
    public partial class MainService : ServiceBase
    {
        public System.Timers.Timer looper;
        DB_CostSplite db_CostSplite = new DB_CostSplite();
        public MainService()
        {
            InitializeComponent();
            InitConfig();
            looper = new System.Timers.Timer();
            looper.Elapsed += new System.Timers.ElapsedEventHandler(looper_Elapsed);
        }
        private void InitConfig()
        {
            string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(path));
        }
        
        void looper_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            looper.Enabled = false;
            try
            {
                List<SyncPlan> planList = db_CostSplite.GetPlanList(); 
                foreach (SyncPlan item in planList)
                {
                    if (GetFitRunCond(item))
                    {
                        ReturnInfo rInfo = null;
                        switch ((SyncType)item.Id)
                        {
                            //期
                            case SyncType.ERP_基础信息:
                                rInfo = new ERP.SyncData().SyncBaseData();
                                break;
                            case SyncType.ERP_明细:
                                rInfo =new ERP.SyncData().SyncContractDetailData(true);
                                break;
                            case SyncType.ERP_明细_累积更新:
                                rInfo =new ERP.SyncData().SyncContractDetailData(false);
                                break;
                            case SyncType.明源_楼栋:
                                rInfo =new MING.SyncData().SyncBuildingData();
                                break;
                            default:
                                break;
                        }
                       db_CostSplite.Record(item, rInfo);
                    }

                    
                }
                LoggerWorker.logger.DebugFormat("OK");
            }
            catch (Exception ex)
            {
                LoggerWorker.logger.DebugFormat("{0}\r\n{1}", ex.Message, ex.StackTrace);
            }
            finally
            {

            }
            looper.Enabled = true;
        }

        private bool GetFitRunCond(SyncPlan item)
        {
            if (item.IsEnable == 0)
            {
                return false;
            }

            if (item.IsJustIn == 1)
            {
                return true;
            }

            if (item.IsJustIn == 0)
            {
                if (item.LastExeTime == null || !item.LastExeTime.HasValue)
                {
                    return true;
                }
                TimeSpan span = DateTime.Now - item.LastExeTime.Value;
                if (span.Hours > item.InnervalTime)
                {
                    return true;
                }
            }
            return false;

        }

        protected override void OnStart(string[] args)
        {
            looper.Interval = 1000;
            looper.Start();
        }

        protected override void OnStop()
        {
            looper.Stop();
        }
    }
}
